package com.youi.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.mapper.ProductMapper;
import com.youi.shop.pojo.entity.Product;
import com.youi.shop.pojo.qo.product.UserUpdateProductQO;
import com.youi.shop.pojo.vo.product.ProductVO;
import com.youi.shop.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    ProductMapper productMapper;

    /**
     * 分页查询所有商品
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public RespEntity<List<ProductVO>> selectByPage(int page, int size) {

        Page<Product> productPage = new Page<>(page, size);
        List<Product> productList = productMapper.selectPage(productPage, null).getRecords();
        List<ProductVO> list = new ArrayList<>();
        //循环遍历存入list中
        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            list.add(productVO);
        }

        return RespEntity.success(list);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public RespEntity<List<ProductVO>> selectAll() {
        //查询所有
        List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().eq("delete_status", 0));

        List<ProductVO> list = new ArrayList<>();
        //存入respEntity返回客户端
        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            list.add(productVO);
        }
        return RespEntity.success(list);
    }

    /**
     * 按分类查询
     *
     * @param categoryId
     * @return
     */
    @Override
    public RespEntity<List<ProductVO>> selectByCategory(Long categoryId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<Product> productList = productMapper.selectList(wrapper);
        List<ProductVO> list = new ArrayList<>();
        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            list.add(productVO);
        }

        return RespEntity.success(list);
    }

    /**
     * 按id批量查询
     *
     * @param productIds
     * @return
     */
    @Override
    public RespEntity<List<ProductVO>> selectByIds(List<Long> productIds) {
//        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        List<Product> productList = productMapper.selectBatchIds(productIds);
//        for (Long productId : productIds) {
//            wrapper.eq("product_id", productId);
//        }
//        List<Product> productList = productMapper.selectList(wrapper);
        List<ProductVO> list = new ArrayList<>();
        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            list.add(productVO);
        }

        return RespEntity.success(list);
    }

    /**
     * 按名字模糊查询
     *
     * @param productName
     * @return
     */
    @Override
    public RespEntity<List<ProductVO>> selectByName(String productName) {
        //模糊查询
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("product_name", productName);

        List<Product> productList = productMapper.selectList(wrapper);
        List<ProductVO> list = new ArrayList<>();
        for (Product product : productList) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);
            list.add(productVO);
        }

        return RespEntity.success(list);
    }

    /**
     * 修改单个商品
     *
     * @param userUpdateProductQO
     * @return
     */
    @Override
    public boolean updateProduct(UserUpdateProductQO userUpdateProductQO) {
        boolean flag = false;
        //查询是否有该商品
        Product product = productMapper.selectById(userUpdateProductQO.getProductId());
        //没有返回false
        if (product == null) {
            return flag;
        }

        BeanUtils.copyProperties(userUpdateProductQO,product);
        System.out.println(product);
        int result = productMapper.updateById(product);
        if (result > 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 批量删除商品
     *
     * @param productQOIds
     * @return
     */
    @Override
    public boolean deleteProduct(List<Long> productQOIds) {
        boolean flag =false;
        int res = productMapper.deleteBatchIds(productQOIds);
        //删除成功
        if (res > 0) {
            flag = true;
        }
        return flag;
    }


}
